package com.liuxing.doctor.controller;

import com.liuxing.doctor.common.Object.ReturnObject;
import com.liuxing.doctor.entity.Doctor;
import com.liuxing.doctor.service.DoctorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @athuer 行柳
 */
@Slf4j
@Controller
public class LoginController {
    @Resource
    private DoctorService service;

    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String login(){
        return "index";
    }
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public Object login(String username, String password, HttpServletRequest request){
        Map<String,Object> map=new HashMap<>();
        map.put("username",username);
        map.put("password",password);
        Doctor doctor=service.queryByIdAndPwd(map);
        ReturnObject returnObject=new ReturnObject();
        if(doctor==null){
            returnObject.setCode("0");
            returnObject.setMessage("用户名或密码错误");
        }else{
            request.getSession().setAttribute("doctor",doctor);
            returnObject.setMessage("登陆成功");
            returnObject.setCode("1");
        }
        return returnObject;
    }

}
